home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / GLE / joinoffset.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  3.3 KB  |  146 lines

  1.  
  2. /* cylinder drawing demo */
  3. /* this demo demonstrates the various join styles */
  4.  
  5. /* required include files */
  6. #include <GL/glut.h>
  7. #include <GL/tube.h>
  8.  
  9. /* ------------------------------------------------------- */
  10.  
  11. /* the arrays in which we will store the polyline */
  12. #define NPTS 100
  13. double points [NPTS][3];
  14. float colors [NPTS][3];
  15. int idx = 0;
  16.  
  17. /* some utilities for filling that array */
  18. #define PSCALE 0.5
  19. #define PNT(x,y,z) {             \
  20.    points[idx][0] = PSCALE * x;     \
  21.    points[idx][1] = PSCALE * y;     \
  22.    points[idx][2] = PSCALE * z;        \
  23.    idx ++;                \
  24. }
  25.  
  26. #define COL(r,g,b) {             \
  27.    colors[idx][0] = r;             \
  28.    colors[idx][1] = g;             \
  29.    colors[idx][2] = b;            \
  30. }
  31.  
  32. /* the arrays in which we will store the contour */
  33. #define NCONTOUR 100
  34. double contour_points [NCONTOUR][2];
  35. int cidx = 0;
  36.  
  37. /* some utilities for filling that array */
  38. #define C_PNT(x,y) {             \
  39.    contour_points[cidx][0] = x;        \
  40.    contour_points[cidx][1] = y;     \
  41.    cidx ++;                \
  42. }
  43.  
  44.  
  45. /* ------------------------------------------------------- */
  46. /* 
  47.  * Initialize a bent shape with three segments. 
  48.  * The data format is a polyline.
  49.  *
  50.  * NOTE that neither the first, nor the last segment are drawn.
  51.  * The first & last segment serve only to determine that angle 
  52.  * at which the endcaps are drawn.
  53.  */
  54.  
  55. void InitStuff (void) {
  56.  
  57.    COL (0.0, 0.0, 0.0);
  58.    PNT (16.0, 0.0, 0.0);
  59.  
  60.    COL (0.2, 0.8, 0.5);
  61.    PNT (0.0, -16.0, 0.0);
  62.  
  63.    COL (0.0, 0.8, 0.3);
  64.    PNT (-16.0, 0.0, 0.0);
  65.  
  66.    COL (0.8, 0.3, 0.0);
  67.    PNT (0.0, 16.0, 0.0);
  68.  
  69.    COL (0.2, 0.3, 0.9);
  70.    PNT (16.0, 0.0, 0.0);
  71.  
  72.    COL (0.2, 0.8, 0.5);
  73.    PNT (0.0, -16.0, 0.0);
  74.  
  75.    COL (0.0, 0.0, 0.0);
  76.    PNT (-16.0, 0.0, 0.0);
  77.  
  78.    C_PNT (-0.8, -0.5);
  79.    C_PNT (-1.8, 0.0);
  80.    C_PNT (-1.2, 0.3);
  81.    C_PNT (-0.7, 0.8);
  82.    C_PNT (-0.2, 1.3);
  83.    C_PNT (0.0, 1.6);
  84.    C_PNT (0.2, 1.3);
  85.    C_PNT (0.7, 0.8);
  86.    C_PNT (1.2, 0.3);
  87.    C_PNT (1.8, 0.0);
  88.    C_PNT (0.8, -0.5);
  89.  
  90.    gleSetJoinStyle (TUBE_JN_ANGLE | TUBE_CONTOUR_CLOSED | TUBE_JN_CAP);
  91. }
  92.  
  93. double up_vector[3] = {1.0, 0.0, 0.0};
  94.  
  95. extern float lastx;
  96. extern float lasty;
  97.  
  98. /* ------------------------------------------------------- */
  99. /* draw the extrusion */
  100.  
  101. void DrawStuff (void) {
  102.    double moved_contour [NCONTOUR][2];
  103.    int style, save_style;
  104.    int i;
  105.  
  106.    for (i=0; i<cidx; i++) {
  107.       moved_contour[i][0] = contour_points [i][0];
  108.       moved_contour[i][1] = contour_points [i][1] + 0.05 * (lasty-200.0);
  109.    }
  110.  
  111.    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  112.  
  113.    /* set up some matrices so that the object spins with the mouse */
  114.    glPushMatrix ();
  115.    glTranslatef (0.0, 4.0, -80.0);
  116.    glRotatef (0.5*lastx, 0.0, 1.0, 0.0);
  117.  
  118.    gleExtrusion (cidx, moved_contour, contour_points, up_vector, 
  119.                  idx, points, colors);
  120.  
  121.    glPopMatrix ();
  122.  
  123.  
  124.    /* draw a seond copy, this time with the raw style, to compare
  125.     * things against */
  126.    glPushMatrix ();
  127.    glTranslatef (0.0, -4.0, -80.0);
  128.    glRotatef (0.5*lastx, 0.0, 1.0, 0.0);
  129.  
  130.    save_style = gleGetJoinStyle ();
  131.    style = save_style;
  132.    style &= ~TUBE_JN_MASK;
  133.    style |= TUBE_JN_RAW;
  134.    gleSetJoinStyle (style);
  135.  
  136.    gleExtrusion (cidx, moved_contour, contour_points, up_vector, 
  137.                  idx, points, colors);
  138.  
  139.    gleSetJoinStyle (save_style);
  140.    glPopMatrix ();
  141.  
  142.    glutSwapBuffers ();
  143. }
  144.  
  145. /* ------------------ end of file ----------------------------- */
  146.